﻿using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Maps.MapControl;
using System.Windows.Input;
using System.Windows.Media;

/*
 * minimap taken from 
 * http://www.earthware.co.uk/blog/index.php/2009/03/virtual-earth-silverlight-minimap-tutorial/
 */

/*
 * how to have a userControl button click execute a main application command found at
 * http://efreedom.com/Question/1-3586881/Bind-KeyUp-Button-Click-Event
 * http://www.blog.ingenuitynow.net/Creating+Custom+Silverlight+4+Controls+That+Support+Command+Binding.aspx
 */
namespace MyFloridaHouse.libSvUIControls
{

    public partial class MiniMap : UserControl
    {
        public MiniMap()
        {
            InitializeComponent();
            LayoutCanvas.DataContext = this;
            myMiniMapControl.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(onMiniMapMouseClick), true);  //Note that last parameter important 

            
        }

        #region Mouse Click on Map
        private void onMiniMapMouseClick(object sender, MouseButtonEventArgs args)
        {
            if (args != null)
            {
                if (CmdMiniMapMouseClick != null)
                {
                    Point mouseClickPoint = args.GetPosition(this);
                    Location mouseClickLocation = myMiniMapControl.ViewportPointToLocation(mouseClickPoint);
                    CmdMiniMapMouseClick.Execute(mouseClickLocation);
                }
            }
        }



        public ICommand CmdMiniMapMouseClick
        {
            get { return GetValue(CmdMiniMapMouseClickProperty) as ICommand; }
            set { SetValue(CmdMiniMapMouseClickProperty, value); }
        }
        
        public static readonly DependencyProperty CmdMiniMapMouseClickProperty =
                DependencyProperty.Register(
                        "MiniMapMouseClickCommand",
                        typeof(ICommand),
                        typeof(MiniMap),
                        null);
        #endregion

        #region BoundingBox Properties
        public Location LatLonNW
        {
            get { return (Location)GetValue(LatLonNWProperty); }
            set { SetValue(LatLonNWProperty, value); }
        }

        public static readonly DependencyProperty LatLonNWProperty =
            DependencyProperty.Register("LatLonNW", typeof(Location), typeof(MiniMap), new PropertyMetadata(new Location(), new PropertyChangedCallback(onLatLonNWChanged)));

        public static void onLatLonNWChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
        {
            MiniMap myMiniMap = d as MiniMap;

            if (myMiniMap != null)
            {
                if (args.NewValue != null)
                {
                    CreateBoundingBox(myMiniMap);
                }
            }

        }

        public Location LatLonSE
        {
            get { return (Location)GetValue(LatLonSEProperty); }
            set { SetValue(LatLonSEProperty, value); }
        }

        public static readonly DependencyProperty LatLonSEProperty =
            DependencyProperty.Register("LatLonSE", typeof(Location), typeof(MiniMap), new PropertyMetadata(new Location(), new PropertyChangedCallback(onLatLonSEChanged)));

        public static void onLatLonSEChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
        {
            MiniMap myMiniMap = d as MiniMap;

            if (myMiniMap != null)
            {
                if (args.NewValue != null)
                {
                    CreateBoundingBox(myMiniMap);

                }
            }

        }

        public static void CreateBoundingBox(MiniMap myMiniMap)
        {
            Location NW = myMiniMap.LatLonNW;
            Location SE = myMiniMap.LatLonSE;
            if ((NW != null) & (SE != null))
            {
                MapPolygon newBB = new MapPolygon();
                newBB.Stroke = new SolidColorBrush(Colors.Black);
                newBB.StrokeThickness = 2.0;
                newBB.Fill = new SolidColorBrush(Colors.Yellow);
                newBB.Opacity = .5;
                newBB.Locations = new LocationCollection();
                newBB.Locations.Add(new Location(NW.Latitude, NW.Longitude));
                newBB.Locations.Add(new Location(SE.Latitude, NW.Longitude));
                newBB.Locations.Add(new Location(SE.Latitude, SE.Longitude));
                newBB.Locations.Add(new Location(NW.Latitude, SE.Longitude));
                newBB.Locations.Add(new Location(NW.Latitude, NW.Longitude));
                // need to insert this shape on the map layer
                MapLayer newLayer = new MapLayer();
                myMiniMap.myMiniMapControl.Children.Clear();
                myMiniMap.myMiniMapControl.Children.Add(newLayer);
                newLayer.Children.Add(newBB);
            }

        }
        #endregion

        #region Center Property

        public Location Center
        {
            get { return (Location)GetValue(CenterProperty); }
            set { SetValue(CenterProperty, value); }
        }

        public static readonly DependencyProperty CenterProperty =
            DependencyProperty.Register("Center", typeof(Location), typeof(MiniMap), new PropertyMetadata(new Location()));

        
        #endregion

        #region ZoomLevel Property

        public double ZoomLevel
        {
            get { return (double)GetValue(ZoomLevelProperty); }
            set { SetValue(ZoomLevelProperty, value); }
        }

        public static readonly DependencyProperty ZoomLevelProperty =
            DependencyProperty.Register("ZoomLevel", typeof(double), typeof(MiniMap), new PropertyMetadata(0d));
        
        #endregion

        #region disabled mouse events
        private void MiniMap_MousePan(object sender, MapMouseDragEventArgs e)
        {
            e.Handled = true;//dont want minimap to be interactive
        }

        private void MiniMap_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            e.Handled = true;//dont want minimap to be interactive
        }

        private void MiniMap_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;//dont want minimap to be interactive
        }

        private void MiniMap_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;//dont want minimap to be interactive
        }
        #endregion

        private void MiniMapMinimize_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            if (MapBorder.Visibility == Visibility.Visible)
            {
                Canvas.SetLeft(miniMapMinimizeButton,0);
                Canvas.SetTop(miniMapMinimizeButton, 90);
                MapBorder.Visibility = Visibility.Collapsed;
            }
            else
            {
                Canvas.SetLeft(miniMapMinimizeButton, 110);
                Canvas.SetTop(miniMapMinimizeButton, 0);
                MapBorder.Visibility = Visibility.Visible;
            }
            
        }

    }

}
